package com.zscat.mallplus.qsg.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zscat.mallplus.qsg.CostDuration;
import com.zscat.mallplus.qsg.IQsgCostDurationService;
import com.zscat.mallplus.qsg.mapper.QsgCostDurationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QsgCostDurationServiceImpl  extends ServiceImpl<QsgCostDurationMapper, CostDuration> implements IQsgCostDurationService {

    @Resource
    private QsgCostDurationMapper costDurationMapper;

    @Override
    public List<CostDuration> getTop10Users() {
        return costDurationMapper.selectTop10Users();
    }

    @Override
    public List<CostDuration> getTop10UsersForMonth(Integer storeId) {
        LocalDate currentDate = LocalDate.now();
        int currentYear = currentDate.getYear();
        int currentMonth = currentDate.getMonthValue();
        LocalDate firstDayOfMonth = currentDate.withDayOfMonth(1);
        //如果当前月是12月则需要把年增加1
        LocalDate firstDayOfNextMonth = null;
        if(currentMonth == 12){
            firstDayOfNextMonth = LocalDate.of(currentYear+1, 1, 1);
        }else{
            firstDayOfNextMonth = LocalDate.of(currentYear, currentMonth + 1, 1);
        }
        LocalDate lastDayOfMonth = firstDayOfNextMonth.minusDays(1);
        String startTime = firstDayOfMonth.toString() + " 00:00:00";
        String endTime = lastDayOfMonth.toString() + " 23:59:59";
        return costDurationMapper.selectTop10UsersForMonth(startTime,endTime,storeId);
    }
}
